<?php
class ProfileController extends BaseController
{
	public function index() 
	{
		$this->view('all');
	}
	
	public function view($name = null)
	{
		$this->registry->template->setMenuKey('profile');
		$tem = $this->registry->template;
		
		if ($name == 'all') {
			$tags['manuContentTitle'] = 'Profile Management';
			$content = $this->getProfTable();
		}
		else {
			$tags['manuContentTitle'] = 'View Profile: ' . $name;
			$atts = array('PROFILE', 'SESSIONS_PER_USER', 'CONNECT_TIME', 'IDLE_TIME');
			$content = $this->getGeneralTbl($name, $atts) . '</br>' .
					   $this->getUserTbl($name);
		}
		
		$tags['path'] = MANU_ROOT_PATH . 'profile';
		$tags['what'] = 'profile';
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);
				
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		
		$output = $this->registry->template->getPageContent('index', 'Oracle User Manager', $content, $tags);
		
		echo $output;
	}
	
	public function getProfTable()
	{
		//some basic info showed
		//TODO:	Need to modify
		$atts = array('PROFILE');
			
		$rows = $this->registry->db->get('profileManager')->getBasicInfo();
		
		$res = $this->getEntityTable($atts, $rows, 'profile');
	
		return $res;
	}
	
	public function getGeneralTbl($name, array $atts = array())
	{
		$prof = $this->registry->db->get('profileManager');
		 
		$res = '<div id="title">General</div><hr>';
		$tbl = '<table id="general-table">';
		$tbl .= '<tr><td class="first-column">PROFILE</td><td class="second-column">' . $name . '</td></tr>';
		
		$rows = $prof->getGenInfo($name);
		foreach ($rows as $row) {
			if (in_array($row->get('RESOURCE_NAME'), $atts)) {
				$key = $row->get('RESOURCE_NAME');
				$val = $row->get('LIMIT');
				$tbl .= '<tr><td class="first-column">' . $key . '</td><td class="second-column">' . $val . '</td></tr>';
			}
		}
		$tbl .= '</table>';
		 
		$res .= $tbl;
		 
		return $res;
	}
	
	/*
	 * @description:	Get Users who are granted profile
	 */
	public function getUserTbl($profile)
	{
		$prof = $this->registry->db->get('profileManager');
		$tem = $this->registry->template;
		
		$atts = array('USERNAME');
		$rows = $prof->getUserOf($profile);
		
		$res = '<div id="title">Users Have The Profile</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);
		
		return $res;
	}
	public  function add(){
		$this->registry->template->setMenuKey('profile');
		
		$tem = $this->registry->template;
		$prof = $this->registry->db->get('profileManager');
		$tags['manuContentTitle'] = 'Add Profile';
		
		$method = $_SERVER['REQUEST_METHOD'];
		
		if ($method == 'POST') {
			if(isset($_POST["name"])) {
				$name = strip_tags($_POST["name"]);
				if ($name == "") {
					header("Location: ../add/name");
					exit();
				}
			}
			if(isset($_POST["session"])) {
				$session = strip_tags($_POST["session"]);
				if ($session == "") {
					header("Location: ../add/session");
					exit();
				}
			}
			if(isset($_POST["connect"])) {
				$connect = strip_tags($_POST["connect"]);
				if ($connect == "") {
					header("Location: ../add/connect");
					exit();
				}
			}
			if(isset($_POST["idle"])) {
				$idle = strip_tags($_POST["idle"]);
				if ($idle == "") {
					header("Location: ../add/idle");
					exit();
				}
			}
						
			$prof->addProfile($name, $session, $connect, $idle);
			
			header("Location: ./index");
			
			exit();
		} 	
		else {
			$create = $prof->checkCreateProfile();
			if($create == false){
				$content = <<<EOT
User don't have CREATE PROFILE Privilege !<br /><br />
EOT;
			}else{
				$tags['name_val'] = "";
				$tags['session_val'] = "";
				$tags['connect_val'] = "";
				$tags['idle_val'] = "";
				$tags['disable'] = null;		
				$content = $tem->getModule('addprofile', $tags);	
			}				
			// get temporary_tablespace names
			
		}
		
		$tags['path'] = MANU_ROOT_PATH . 'profile';
		
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);
				
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		
		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
		
		echo $output;
		
	}
	public  function edit($paraName){
		$this->registry->template->setMenuKey('profile');
		
		$tem = $this->registry->template;
		$prof = $this->registry->db->get('profileManager');
		$tags['manuContentTitle'] = 'Edit Profile';
		
		$method = $_SERVER['REQUEST_METHOD'];
		
		if ($method == 'POST') {
			if(isset($_POST["name"])) {
				$name = strip_tags($_POST["name"]);
				if ($name == "") {
					header("Location: ../add/name");
					exit();
				}
			}
			if(isset($_POST["session"])) {
				$session = strip_tags($_POST["session"]);
				if ($session == "") {
					header("Location: ../add/session");
					exit();
				}
			}
			if(isset($_POST["connect"])) {
				$connect = strip_tags($_POST["connect"]);
				if ($connect == "") {
					header("Location: ../add/connect");
					exit();
				}
			}
			if(isset($_POST["idle"])) {
				$idle = strip_tags($_POST["idle"]);
				if ($idle == "") {
					header("Location: ../add/idle");
					exit();
				}
			}
						
			$prof->editProfile($paraName, $session, $connect, $idle);
			
			header("Location: ../index");
			
			exit();
		} 	
		else {
			// get temporary_tablespace names
			$create = $prof->checkProfile('CREATE PROFILE');
			if($create == false){
				$content = <<<EOT
User don't have ALTER PROFILE Privilege !<br /><br />
EOT;
			}else{
				$res = $prof->getGenInfo($paraName);
				$tags['connect_val'] = "";
				$tags['idle_val'] = "";
				$tags['session_val'] ="";
				foreach ( $res AS $ele ){
					if($ele->get('RESOURCE_NAME')  == 'SESSIONS_PER_USER')
						$tags['session_val'] = $ele->get('LIMIT');
					if($ele->get('RESOURCE_NAME')  == 'CONNECT_TIME')
						$tags['connect_val'] = $ele->get('LIMIT');
					if($ele->get('RESOURCE_NAME')  == 'IDLE_TIME')
						$tags['idle_val'] = $ele->get('LIMIT');
				}
				
				$tags['name_val'] = $paraName;
				$tags['disable'] = "disabled=\"disabled\"";		
				$content = $tem->getModule('addprofile', $tags);
			}
			
		}
		
		$tags['path'] = MANU_ROOT_PATH . 'profile';
		
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);
				
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		
		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
		
		echo $output;
		
	}
	
	/*
	 * @description:	Delete profile
	 */
	public function delete($profile)
	{
		$man = $this->registry->db->get('profileManager');
		$userMan = $this->registry->db->get('userManager');
		$tem = $this->registry->template;
		
		$drop = $userMan->checkPriv('DROP PROFILE');
		if($drop == false){
			$tem->setMenuKey('profile');
			
			$content = <<<EOT
User don't have DROP PROFILE Privilege !<br /><br />
EOT;
			$tags['manuContentTitle'] = 'Drop Profile';
			$tags['manuMessage'] = '';
				
			$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
				
			echo $output;
		}
		else {
			$man->deleteProfile($profile);
			header("Location: ../index");		
			exit();
		}
	}
}
?>